package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.api.mongo.VideoApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.mongo.FollowUser;
import com.tanhua.domain.mongo.Video;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoMqService {
    @Reference
    private VideoApi videoApi;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    public void sendMsg(String publishId, int type) {
        Long userId=UserHolder.getUser().getId();
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("type",type);
        Video video=videoApi.findbyUid(publishId);
        System.out.println("生产者"+video);
        map.put("vid",video.getVid());
        rocketMQTemplate.convertAndSend("tanhua-video",map);
        System.out.println("消息中间件 生产了");
    }

    public void sendMsg(Long uid, int type) {
        //发表视屏默认为2分
        Long userId=UserHolder.getUser().getId();
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("type",type);
        Video video=videoApi.findbyUid(uid);
        System.out.println("生产者"+video);
        map.put("vid",video.getVid());
        rocketMQTemplate.convertAndSend("tanhua-video",map);

        //给关注的人加分
        List<FollowUser> followUserList = videoApi.findFollowUserList(userId);
        if(CollectionUtil.isNotEmpty(followUserList)){
            for (FollowUser followUser : followUserList) {
                Map followmap=new HashMap();
                followmap.put("userId",followUser.getFollowUserId());
                followmap.put("type",2);
                System.out.println("生产者--关注用户刚发布的"+video);
                followmap.put("vid",video.getVid());
                rocketMQTemplate.convertAndSend("tanhua-video",followmap);
            }
        }

        System.out.println("消息中间件 生产了");
    }

    public void sendMsg2(Long uid, int type){
        List<Video> videoList = videoApi.findVideoList(uid);
        if(CollectionUtil.isNotEmpty(videoList)){
            for (Video video : videoList) {
                Map videomap=new HashMap();
                videomap.put("userId",uid);
                videomap.put("type",type);
                System.out.println("生产者--关注用户已经发布的"+video);
                videomap.put("vid",video.getVid());
                rocketMQTemplate.convertAndSend("tanhua-video",videomap);
            }
        }
        System.out.println("消息中间件 生产了");
    }
}
